package com.cssnj.portal.web.admin.rolefunction;

import com.cssnj.portal.domain.common.UserInfo;
import com.cssnj.portal.service.admin.positionrole.PositionroleService;
import com.cssnj.portal.service.admin.rolefunction.RolefunctionService;
import com.cssnj.portal.service.common.TreeService;
import com.cssnj.portal.service.home.HomeService;
import io.predictech.framework.domain.common.JsonRequest;
import io.predictech.framework.domain.common.JsonResponse;
import io.predictech.framework.domain.exception.BaseException;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping({"/admin/rolefunction"})
public class RolefunctionController {

    @Autowired
    private RolefunctionService rolefunctionService;

    @Autowired
    private TreeService treeService;

    @Autowired
    Environment environment;


    @RequestMapping({"/init.do"})
    public ModelAndView init(HttpServletRequest request) throws BaseException, SQLException {
        //页面初始化
        JsonRequest jsonRequest = new JsonRequest();
        jsonRequest.setServiceName("rolefunctionService");
        jsonRequest.setHandleCode("init");
        JsonResponse jsonResponse = rolefunctionService.execute(jsonRequest);
        ModelAndView modelAndView = new ModelAndView("admin/rolefunction/rolefunction");
        if (null != jsonResponse.getRepData().get("jsList")) {
            modelAndView.getModelMap().put("jsList", jsonResponse.getRepData().get("jsList"));
        }
        return modelAndView;
    }

    @RequestMapping({"/queryGnListByJsId.do"})
    @ResponseBody
    public JsonResponse queryGnListByJsId(HttpServletRequest request) throws BaseException, SQLException {
        //根据角色id查询功能树(调用公共方法)
        JsonRequest jsonRequest = new JsonRequest();
        jsonRequest.setServiceName("treeService");
        jsonRequest.setHandleCode("gnJsTree");
        jsonRequest.getReqData().put("role", request.getParameter("jsid"));
        JsonResponse jsonResponse = treeService.execute(jsonRequest);
        return jsonResponse;
    }

    @RequestMapping({"/saveData.do"})
    @ResponseBody
    public JsonResponse saveData(HttpServletRequest request) throws BaseException, SQLException {
        //保存数据
        JsonRequest jsonRequest = new JsonRequest();
        jsonRequest.setServiceName("rolefunctionService");
        jsonRequest.setHandleCode("saveData");
        jsonRequest.getReqData().put("jsid", request.getParameter("jsid"));
        jsonRequest.getReqData().put("str_gnlist", request.getParameter("str_gnlist"));
        UserInfo userInfo = (UserInfo) SecurityUtils.getSubject().getPrincipal();
        jsonRequest.getReqData().put("swrydm", userInfo.getSwryDm());
        JsonResponse jsonResponse = rolefunctionService.execute(jsonRequest);
        return jsonResponse;
    }
}
